package com.graduce.eduorder.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.graduce.commonutils.JwtUtils;
import com.graduce.commonutils.R;
import com.graduce.eduorder.entity.Order;
import com.graduce.eduorder.entity.vo.OrderQuery;
import com.graduce.eduorder.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-04-18
 */
@RestController
@RequestMapping("/eduorder/order")

public class OrderController {
    @Autowired
    private OrderService orderService;

    //1 生成订单的方法
    @PostMapping("createOrder/{courseId}")
    public R saveOrder(@PathVariable String courseId, HttpServletRequest request) {
        //创建订单，返回订单号
        String orderNo =
                orderService.createOrders(courseId,JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("orderId",orderNo);
    }

    //2 根据订单id查询订单信息
    @GetMapping("getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable String orderId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderId);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("item",order);
    }

    //根据课程id和用户id查询订单表中订单状态
    @GetMapping("isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable String courseId,@PathVariable String memberId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",memberId);
        wrapper.eq("status",1);//支付状态 1代表已经支付
        int count = orderService.count(wrapper);
        if(count>0) { //已经支付
            return true;
        } else {
            return false;
        }
    }
    //分页查询订单信息
    @GetMapping("pageOrder/{current}/{limit}")
    public R pageOrder(@PathVariable long current,
                             @PathVariable long limit){
//        创建配置对象
        Page<Order> pageOrder= new Page<>(current,limit);
//        调用方法实现分页
//        调用方法，底层封装，把分页所有数据封装
        orderService.page(pageOrder,null);
        long total = pageOrder.getTotal();
        List<Order> records= pageOrder.getRecords();
        return R.ok().data("total",total).data("records",records);
    }

    @PostMapping("pageOrderCondition/{current}/{limit}")
    public R pageOrderCondition(@PathVariable long current, @PathVariable long limit,
                                 @RequestBody(required = false) OrderQuery orderQuery){
        Page<Order> pageOrder = new Page<>(current,limit);

        //构建条件
        QueryWrapper<Order> wrapper = new QueryWrapper<>();

        //多条件查询
        String teacher_name = orderQuery.getTeacher_name();
        String status = orderQuery.getStatus();

        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(teacher_name)) {
            //构建条件
            wrapper.like("teacher_name",teacher_name);
        }
        if(!StringUtils.isEmpty(status)) {
            wrapper.eq("status",status);
        }

        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        orderService.page(pageOrder,wrapper);
        long total = pageOrder.getTotal();//总记录数
        List<Order> records = pageOrder.getRecords(); //数据list集合
        return R.ok().data("total",total).data("rows",records);

    }

    @ApiOperation(value = "删除订单")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id) {
        orderService.removeById(id);
        return R.ok();
    }
}

